//
//  JSMTouringViewController.m
//  TakeYouTourTogether
//
//  Created by king on 15/11/26.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMTouringViewController.h"
#import "JSMSearchViewController.h"
#import "JSMTouringModel.h"
#import "JSMTouringCell.h"

#import "JSMDetailsViewController.h"
@interface JSMTouringViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)BaseTableView *touringTableView;
@property(nonatomic, strong)NSMutableArray *touringArr;
@property(nonatomic, assign) NSInteger TouringUrlPage;
@end

@implementation JSMTouringViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createRightButton];
    //下拉刷新
    self.touringTableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    [self.touringTableView.header beginRefreshing];
    //下拉加载
    self.touringTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.title = LanguageTool(@"一起向");
}
#pragma mark - 刷新加载ba
- (void)headerRefresh{
    self.TouringUrlPage = 1;
    [self createDate];
}
- (void)footerRefresh{
    self.TouringUrlPage++;
    [AFNetworkingTool getUrl:[NSString stringWithFormat:@"http://tubu.ibuzhai.com/rest/v1/travelog/recommends?&app_version=4.3.3&page_size=20&type=19&api_version=1&page=%ld&device_type=2",self.TouringUrlPage] hudSuperView:self.view result:JSON success:^(id result) {
        NSDictionary *dic = result;
        NSMutableArray *arr = [JSMTouringModel baseModelByArr:dic[@"logs"]];
        [self.touringArr addObjectsFromArray:arr];
        [self.touringTableView reloadData];
        [self.touringTableView.footer endRefreshing];
        
    } failure:^(NSError *error){
        
    }];
 }

#pragma mark - 解析数据
- (void)createDate{
    [self.touringTableView.header endRefreshing];
    self.touringArr = [JSMCollectionOfMessage getModelToPlistType:CollectionOfMessageTypeTouringModel];
    [self.touringTableView reloadData];
    [AFNetworkingTool getUrl:@"http://tubu.ibuzhai.com/rest/v1/travelog/recommends?&app_version=4.3.3&page_size=20&type=19&api_version=1&page=1&device_type=2" hudSuperView:self.view result:JSON success:^(id result) {
        NSDictionary *dic = result;
        self.touringArr = [JSMTouringModel baseModelByArr:dic[@"logs"]];
        [JSMCollectionOfMessage saveModelToPlist:self.touringArr type:CollectionOfMessageTypeTouringModel];
        self.touringTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        [self.touringTableView reloadData];
        
    } failure:^(NSError *error){
    }];
}

#pragma mark - TouringTableView
- (BaseTableView *)touringTableView{
   
    if (!_touringTableView) {
        _touringTableView = [[BaseTableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        _touringTableView.dataSource = self;
        _touringTableView.delegate = self;
        [self.view addSubview:_touringTableView];
        _touringTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_touringTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
    }
    return _touringTableView;
}

#pragma mark - rightButton
- (void)createRightButton {
    BaseButton *searchButton = [BaseButton buttonWithType:UIButtonTypeCustom];
    searchButton.frame = CGRectMake(0, 0, 40, 40);
    [searchButton setImage:[UIImage imageNamed:@"search.png"] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:searchButton];
    [searchButton addTarget:self action:@selector(searchButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}


#pragma mark - tableView协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return self.touringArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *str = @"reuse";
    JSMTouringCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[JSMTouringCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];
    }
    [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:[self.touringArr[indexPath.row] cover]] placeholderImage:[UIImage imageNamed:@"placeHolderImage"]];
    [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:[[self.touringArr[indexPath.row] userModel] avatar]] placeholderImage:[UIImage imageNamed:@"placeHolderImage"]];
    cell.titleLabel.text = [self.touringArr[indexPath.row] name];
    cell.userNameLabel.text = [NSString stringWithFormat:@"%@ · %@ · %@", [[self.touringArr[indexPath.row] userModel] nickname], [self.touringArr[indexPath.row] destination], [self.touringArr[indexPath.row] start_date]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}

#pragma mark - 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    JSMDetailsViewController *jsmDetailVC = [[JSMDetailsViewController alloc]init];
    jsmDetailVC.detailModel = self.touringArr[indexPath.row];
    jsmDetailVC.str_title = [self.touringArr[indexPath.row] destination];
    jsmDetailVC.navigationItem.hidesBackButton = YES;
    [self.navigationController pushViewController:jsmDetailVC animated:YES];
}
#pragma mark 搜索
- (void)searchButtonAction:(UIButton *)searchButton {
    JSMSearchViewController *searchVC = [[JSMSearchViewController alloc]init];
    [self.navigationController pushViewController:searchVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
